package com.huijz.flywaydemo.controller;

import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/flyway")
public class FlywayDemoController {

    @Autowired
    private DataSource dataSource;
    
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 获取Flyway迁移信息
     */
    @GetMapping("/info")
    public Map<String, Object> getFlywayInfo() {
        Flyway flyway = Flyway.configure().dataSource(dataSource).load();
        
        Map<String, Object> info = new HashMap<>();
        info.put("current", flyway.info().current().getVersion().toString());
        info.put("migrations", flyway.info().all().length);
        info.put("pending", flyway.info().pending().length);
        
        return info;
    }
    
    /**
     * 手动触发Flyway迁移
     */
    @PostMapping("/migrate")
    public Map<String, Object> migrate() {
        Flyway flyway = Flyway.configure().dataSource(dataSource).load();
        int migrations = flyway.migrate().migrationsExecuted;
        long migrationTimes = flyway.migrate().getTotalMigrationTime();

        Map<String, Object> result = new HashMap<>();
        result.put("migrationsExecuted", migrations);
        result.put("current", flyway.info().current().getVersion().toString());
        result.put("migrationTimes", migrationTimes);

        return result;
    }
    
    /**
     * 查看数据库表结构
     */
    @GetMapping("/schema")
    public Map<String, Object> getSchema() {
        List<Map<String, Object>> tables = jdbcTemplate.queryForList(
                "SELECT table_name FROM information_schema.tables WHERE table_schema = 'PUBLIC'");
        
        Map<String, Object> schema = new HashMap<>();
        schema.put("tables", tables);
        
        return schema;
    }
    
    /**
     * 演示：手动修复失败的迁移
     */
    @PostMapping("/repair")
    public Map<String, Object> repair() {
        Flyway flyway = Flyway.configure().dataSource(dataSource).load();
        flyway.repair();
        
        Map<String, Object> result = new HashMap<>();
        result.put("status", "repaired");
        result.put("current", flyway.info().current().getVersion().toString());
        
        return result;
    }
    
    /**
     * 演示：验证迁移
     */
    @GetMapping("/validate")
    public Map<String, Object> validate() {
        Flyway flyway = Flyway.configure().dataSource(dataSource).load();
        flyway.validate();
        
        Map<String, Object> result = new HashMap<>();
        result.put("status", "valid");
        
        return result;
    }
}